'''
Created on 24/03/2010

@author: eh01
'''
from mako import exceptions
from mako.lookup import TemplateLookup
from mako.template import Template
from datastore.uri import Uri

class MakoHTTPTemplate(object):
    """This class is a wrapper around Mako to allow generation of text (usually XML or HTML) from templates
    and a resource.

    *Usage examples*::

        mako_http_template = MakoHTTPTemplate(spaces, site_template_rs, page_template_res, rs)
        html_data = mako_http_template.render()

    *Common Arguments for Class methods*:
        <arguments>

    *Audience*:
        <audience>

    *Constants*:
        .. data:: constant_1

            constant_1 is used for ......

        .. data:: constant_2

            constant_2 is used for ......
    """

    def __init__(self, spaces, site_template_res, page_template_res, data_res):
        """
        :param spaces: Instance of Spaces
        :param site_template_res: The resource for the site template. The site template is the master/parent/primary template for all the pages in the site.
        :param page_template_res: The resource for the page template. The page template is the template for a particular page in the site.
        :param data_res: The resouce containing the data for this view.
        """
        self.spaces = spaces
        self.site_template_res = site_template_res
        self.page_template_res = page_template_res
        self.data_res = data_res
        self.mako_template_lookup = MakoTemplateLookup(self.spaces)
        self.site_template = Template(site_template_res.content)
        self.page_template = Template(page_template_res.content, lookup=self.mako_template_lookup)

    def render(self):
        """Combines the template and the data to generate the output."""
        try:
            page_template_result = self.page_template.render(data_res=self.data_res)
            site_template_result = self.site_template.render(body=page_template_result,data_res=self.data_res)
            return site_template_result
        except:
            print exceptions.text_error_template().render()


class MakoTemplateLookup(TemplateLookup):
    """This class extends the original TemplateLookup in Mako. Mako allows the <%include> directive which looks up templates on the filesystem
    to include in the parent template. The original TemplateLookup is responsible for this, specifically the 'get_template' method. This is why
    I have overrided the original implementation here to go to Spaces instead to retrieve the template.

    *Usage examples*::

        self.mako_template_lookup = MakoTemplateLookup(self.spaces)
        self.page_template = Template(page_template_res.content, lookup=self.mako_template_lookup)

    *Common Arguments for Class methods*:
        <arguments>

    *Audience*:
        <audience>

    *Constants*:
        .. data:: constant_1

            constant_1 is used for ......

        .. data:: constant_2

            constant_2 is used for ......
    """

    def __init__(self, spaces):
        TemplateLookup.__init__(self)
        self.spaces = spaces

    def get_template(self, uri):
        uri_object = Uri(uri)
        template_res = self.spaces.get(uri_object)
        template = Template(template_res.content)
        return template
